﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace UI.Forms
{
    public partial class sk : FormBase1
    {
        private BindingList<SVC.client.View_Client_lite> clients;
        private BindingList<SVC.client.View_Emp_lite> emps;
        private BindingList<SVC.client.bank> banks;
        BindingList<SVC.client.SK_D> skd;
        BindingList<SVC.client.SK_D> skd_f;
        BindingList<SVC.client.SK_I> ski;
        
        public sk()
        {
            InitializeComponent();
        }

        private void sk_Load(object sender, EventArgs e)
        {
            skd = new BindingList<SVC.client.SK_D>();
            ski = new BindingList<SVC.client.SK_I>();
            clients=my.bw.View_Client_lite_List(false, my.ACID);
            emps = my.bw.View_Emp_lite_List("", my.ACID);
            banks = my.bw.bank_Get("", my.ACID);
            lue_Client.Properties.DataSource = clients;
            lue_JSempid.Properties.DataSource = emps;
            lue_opID.Properties.DataSource = emps;
            lue_opID.EditValue = my.emp_lite.EmplID;
            lue_opID.Properties.ReadOnly = true;
            rlue_zftype.DataSource = my.bw.ZFTYPE_Get("", my.ACID);
            rlue_bank.DataSource=banks;
            rlue_billtype.DataSource=my.bw.Bill_Get(my.ACID);
            gc_sk_D.DataSource = skd;
            gc_sk_I.DataSource = ski;
            //rce_selected.CheckedChanged += new EventHandler(rce_selected_CheckedChanged);
            gridView1.ValidateRow += new DevExpress.XtraGrid.Views.Base.ValidateRowEventHandler(gridView1_ValidateRow);

        }

        void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            SVC.client.SK_D d= e.Row as SVC.client.SK_D;

                if (d.blance>=d.jsje)
                {
                    e.Valid = true;
                    decimal ys = 0;
                    decimal zr = 0;
                    decimal ye = 0;
                    decimal js = 0;
                    foreach (SVC.client.SK_D item in skd)
                    {
                        ys += item.ysje;
                        zr += item.discount.Value;
                        ye += item.blance;
                        js += item.jsje;
                    }
                    sp_ysje.EditValue = ys;
                    sp_ye.EditValue = ye;
                    sp_ss.EditValue = js;
                }
                else
                {
                    e.ErrorText = "结算金额大于应收余额！请检查输入！";
                    e.Valid = false;
                }

        }



        private void dateEdit1_EditValueChanged(object sender, EventArgs e)
        {
            te_pid.Text = my.bw.getBillNO(my.emp_lite.shopcode, dateEdit1.DateTime, 11, my.ACID);
        }

        private void lue_Client_EditValueChanged(object sender, EventArgs e)
        {
            if (lue_Client.EditValue!=null)
            {
                skd.Clear();

                gridView2.OptionsBehavior.Editable = true;
                gridView2.OptionsBehavior.AllowAddRows = gridView1.OptionsBehavior.AllowDeleteRows = DevExpress.Utils.DefaultBoolean.True;
                gridView2.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            }
            else
            {
                
                gridView2.OptionsBehavior.Editable = false;
                gridView2.OptionsBehavior.AllowAddRows = gridView1.OptionsBehavior.AllowDeleteRows = DevExpress.Utils.DefaultBoolean.True;
                gridView2.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            }
        }

        private void btn__Click(object sender, EventArgs e)
        {

            if (skd.Count(p=>p.selected)==0)
            {
                Comm.ShowErorr("请录入要付款的单据！");
                return;
            }
            if (ski.Count==0)
            {
                Comm.ShowErorr("请录入账户入账信息！");
                return;
            }
            decimal skisum = ski.Sum(p => p.jine);
            decimal skdsum = skd.Where(p => p.selected).Sum(p => p.jsje);
            if (skisum!=skdsum)
            {
                Comm.ShowErorr("请检查计算金额与入账金额是否一致！");
                return;
            }
            if (!dvp.Validate()) return;
            SVC.client.SK m=new SVC.client.SK();
            m.ClientID=new Guid(lue_Client.EditValue.ToString());
            m.empid=new Guid(lue_JSempid.EditValue.ToString());
            m.Intro=textEdit1.Text;
            m.opid=new Guid(lue_opID.EditValue.ToString());
            m.Pdate=dateEdit1.DateTime;
            m.pid=te_pid.Text;
            m.remarks=textEdit2.Text;
            m.ssje=Convert.ToDecimal(sp_ss.EditValue);
            m.ysje=Convert.ToDecimal(sp_ysje.EditValue);
            m.zrje=0;
            skd_f.Clear();
            foreach (var item in skd.Where(p=>p.selected))
            {
                skd_f.Add(item);
            }
            string r = my.bw.sk_all_add(m, skd_f, ski, my.ACID);
            if (!string.IsNullOrEmpty(r))
            {
                Comm.ShowErorr(r);
            }
            else
            {
                Comm.ShowInfo("保存成功！");
                resetUI();
            }
        }

        private void resetUI()
        {
            dateEdit1.EditValue = null;
            te_pid.Text = "";
            textEdit1.Text = "";
            textEdit2.Text = "";
            lue_Client.EditValue = null;
            lue_JSempid.EditValue = null;
            ski.Clear();
            skd.Clear();
            lue_opID.EditValue = null; sp_ss.EditValue = sp_ye.EditValue = sp_ysje.EditValue = 0;
        }

        private void btn_save_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (skd.Count(p => p.selected) == 0)
            {
                Comm.ShowErorr("请录入要付款的单据！");
                return;
            }
            if (ski.Count == 0)
            {
                Comm.ShowErorr("请录入账户入账信息！");
                return;
            }
            decimal skisum = ski.Sum(p => p.jine);
            decimal skdsum = skd.Where(p => p.selected).Sum(p => p.jsje);
            if (skisum != skdsum)
            {
                Comm.ShowErorr("请检查计算金额与入账金额是否一致！");
                return;
            }
            if (!dvp.Validate()) return;
            SVC.client.SK m = new SVC.client.SK();
            m.ClientID = new Guid(lue_Client.EditValue.ToString());
            m.empid = new Guid(lue_JSempid.EditValue.ToString());
            m.Intro = textEdit1.Text;
            m.opid = new Guid(lue_opID.EditValue.ToString());
            m.Pdate = dateEdit1.DateTime;
            m.pid = te_pid.Text;
            m.remarks = textEdit2.Text;
            m.ssje = Convert.ToDecimal(sp_ss.EditValue);
            m.ysje = Convert.ToDecimal(sp_ysje.EditValue);
            m.zrje = 0;
            skd_f = new BindingList<SVC.client.SK_D>();
            skd_f.Clear();
            foreach (var item in skd.Where(p => p.selected))
            {
                skd_f.Add(item);
            }
            string r = my.bw.sk_all_add(m, skd_f, ski, my.ACID);
            if (!string.IsNullOrEmpty(r))
            {
                Comm.ShowErorr(r);
            }
            else
            {
                Comm.ShowInfo("保存成功！");
                resetUI();
            }
        }


        private void btn_select_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (lue_Client.EditValue == null)
            {
                Comm.ShowAlert("请先选择一个客户！");
                return;
            }

            选择应收单据 frm = new 选择应收单据(new Guid(lue_Client.EditValue.ToString()));
            frm.FatherRefresh = bindselect;
            frm.ShowDialog();
        }

        void bindselect(List<SVC.client.SK_D> ds)
        {
            skd.Clear();
            decimal ys = 0;
            decimal zr = 0;
            decimal ye = 0;
            decimal js = 0;
            foreach (SVC.client.SK_D item in ds)
            {
                skd.Add(item);
                    ys += item.ysje;
                    zr += item.discount.Value;
                    ye += item.blance;
                    js += item.jsje;
            }
            sp_ysje.EditValue = ys;
            sp_ye.EditValue = ye;
            sp_ss.EditValue = js;
        }
    }
}